package com.indusfo.spc.service.impl;


import com.indusfo.spc.mapper.DeviceTypeInspectMapper;
import com.indusfo.spc.mapper.DeviceTypeMapper;
import com.indusfo.spc.pojo.DeviceType;
import com.indusfo.spc.pojo.DeviceTypeInspect;
import com.indusfo.spc.service.DeviceTypeInspectService;
import com.indusfo.spc.vo.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName DeviceTypeInspectServiceImpl
 * @Description 设备类型点检项目service
 * @Author louk
 * @Date 2019/12/11 8:50
 * @Version 1.0
 */
@Service
public class DeviceTypeInspectServiceImpl implements DeviceTypeInspectService {

    private  static  final Logger logger = LoggerFactory.getLogger(DeviceTypeInspectServiceImpl.class);

    @Resource
    DeviceTypeInspectMapper deviceTypeInspectMapper;

    @Resource
    DeviceTypeMapper deviceTypeMapper;
    /**
     * 批量新增设备类型点检项目明细
     * @param inspectIds
     * @param deviceTypeId
     * @return
     */
    @Override
    public JSONObject updateDeviceTypeInspectList(Integer[] inspectIds, Integer deviceTypeId) {

        if(inspectIds.length==0 || inspectIds==null){
            return JSONObject.build(JSONObject.ERROR,"未选择点检项目");
        }
        if(deviceTypeId==null || "".equals(deviceTypeId)){
            //获取所有的物料类别进行构建树
            List<DeviceType> list = deviceTypeMapper.queryAllDeviceType(new DeviceType());
            deviceTypeId=list.get(0).getDeviceTypeId();
        }
        int updateCount = deviceTypeInspectMapper.updateDeviceTypeInspectList(inspectIds, deviceTypeId);

        if(updateCount>0){
            return JSONObject.build(JSONObject.SUCCESS,"新增成功");
        }
        return JSONObject.build(JSONObject.ERROR,"新增失败");
    }

    /**
     * 删除设备类型点检项目明细
     * @param deviceTypeInspectId
     * @return
     */
    @Override
    public JSONObject defineDeviceTypeInspect(Integer deviceTypeInspectId,Integer dataState) {
        if(deviceTypeInspectId==null || "".equals(deviceTypeInspectId)){
            return JSONObject.build(JSONObject.ERROR,"未选择设备类型保养项目");
        }

        int defineCount = deviceTypeInspectMapper.defineDeviceTypeInspect(deviceTypeInspectId, dataState);
        if(defineCount>0){
                return JSONObject.build(JSONObject.SUCCESS,"删除成功");
        }
        return JSONObject.build(JSONObject.ERROR,"删除失败");
    }

    /**
     * 批量删除设备类型点检项目明细
     * @param deviceTypeInspectIds
     * @return
     */
    @Override
    public JSONObject defineDeviceTypeInspectList(Integer[] deviceTypeInspectIds,Integer dataState) {
        if(deviceTypeInspectIds==null || deviceTypeInspectIds.length==0){
            return JSONObject.build(JSONObject.ERROR,"未选择设备类型保养项目");
        }

        int defineCount = deviceTypeInspectMapper.defineDeviceTypeInspectList(deviceTypeInspectIds, dataState);
        if(defineCount>0){
            return JSONObject.build(JSONObject.SUCCESS,"删除成功");
        }
        return JSONObject.build(JSONObject.ERROR,"删除失败");
    }
    /**
     * 查询设备类型保养项目明细
     * @param deviceTypeInspect
     * @return
     */
    @Override
    public JSONObject queryByDeviceType(DeviceTypeInspect deviceTypeInspect) {

        Integer pagesize = deviceTypeInspect.getPagesize();
        Integer pageindex = deviceTypeInspect.getPageindex();
        if(pagesize != null && pageindex != null) {
            deviceTypeInspect.setIncept(pagesize*(pageindex - 1));
        }
        /*Integer deviceTypeId = deviceTypeInspect.getDeviceTypeId();
        if(deviceTypeId==null || "".equals(deviceTypeId)){
            //获取所有的物料类别进行构建树
            List<DeviceType> list = deviceTypeMapper.queryAllDeviceType(new DeviceType());
            if(list.isEmpty()){
                return JSONObject.oK("暂无数据");
            }
            deviceTypeInspect.setDeviceTypeId(list.get(0).getDeviceTypeId());
        }*/
        List<DeviceTypeInspect> deviceTypeInspectList = deviceTypeInspectMapper.queryDeviceTypeInspectList(deviceTypeInspect);
        if(!deviceTypeInspectList.isEmpty()){
            int listCount = deviceTypeInspectMapper.countDeviceTypeInspectList(deviceTypeInspect);
            return JSONObject.oK("查询成功",deviceTypeInspectList,listCount);
        }
        return JSONObject.oK("没有相关数据",deviceTypeInspectList,null);
    }
}
